package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.entity.*;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.ScoreVO;
import com.tianya.service.*;
import com.tianya.mapper.ScoreMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【score(成绩表)】的数据库操作Service实现
* @createDate 2025-05-24 19:00:02
*/
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score>
    implements ScoreService{

    @Resource
    private StudentService studentService;

    @Resource
    private CourseService courseService;

    @Resource
    private ExamService examService;

    @Resource
    private ClazzService  clazzService;

    @Override
    public List<ScoreVO> listByStudentId(Long studentId) {
        List<Score> scorePage = list(new QueryWrapper<Score>()
                .eq("student_id", studentId));
        List<ScoreVO> scoreVOList = new ArrayList<>();
        for (Score score : scorePage) {
            Long courseId = score.getCourseId();
            Long examId = score.getExamId();
            Long clazzId = score.getClazzId();

            Course course = courseService.getById(courseId);
            Clazz clazz = clazzService.getById(clazzId);
            Exam exam = examService.getById(examId);
            Student student = studentService.getById(studentId);
            ScoreVO scoreVO = new ScoreVO();
            BeanUtil.copyProperties(score, scoreVO);
            scoreVO.setCourseName(course!=null?course.getName():"unKnown");
            scoreVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
            scoreVO.setExamName(exam!=null?exam.getName():"unKnown");
            scoreVO.setStudentName(student!=null?student.getName():"unKnown");

            scoreVOList.add(scoreVO);
        }
        return scoreVOList;
    }

    @Override
    public PageResultVO<ScoreVO> pageScore(Integer pageNum, Integer pageSize, Score scoreRequestDTO) {
        Page<Score> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(scoreRequestDTO.getClazzId() != null,"clazz_id",  scoreRequestDTO.getClazzId())
                .eq(scoreRequestDTO.getExamId() != null,"exam_id",  scoreRequestDTO.getExamId());
        Page<Score> scorePage = page(page, queryWrapper);
        List<ScoreVO> scoreVOList = new ArrayList<>();
        for (Score score : scorePage.getRecords()) {
            Long courseId = score.getCourseId();
            Long studentId = score.getStudentId();
            Long examId = score.getExamId();
            Long clazzId = score.getClazzId();

            Course course = courseService.getById(courseId);
            Clazz clazz = clazzService.getById(clazzId);
            Exam exam = examService.getById(examId);
            Student student = studentService.getById(studentId);
            ScoreVO scoreVO = new ScoreVO();
            BeanUtil.copyProperties(score, scoreVO);
            scoreVO.setCourseName(course!=null?course.getName():"unKnown");
            scoreVO.setClazzName(clazz!=null?clazz.getName():"unKnown");
            scoreVO.setExamName(exam!=null?exam.getName():"unKnown");
            scoreVO.setStudentName(student!=null?student.getName():"unKnown");

            scoreVOList.add(scoreVO);
        }

        PageResultVO<ScoreVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(scorePage.getTotal());
        pageVO.setRecords(scoreVOList);
        return pageVO;
    }
}




